#!/bin/sh

. /etc/rc.freenas

cifs_file="/tmp/.cifs_AD"
status_file="/var/run/directoryservice.activedirectory"
service=/usr/sbin/service
python=/usr/local/bin/python
notifier=/usr/local/bin/midclt

adctl_cmd()
{
	local args="$*"

	if [ -n "${args}" ]
	then
		logger -t ActiveDirectory "${args}"
		${args}
		return $?
	fi

	return 0
}

sssd_running()
{
	${service} sssd onestatus >/dev/null 2>&1
	return $?
}

sssd_start()
{
	adctl_cmd ${service} sssd onestart
	return $?
}

sssd_stop()
{
	adctl_cmd ${service} sssd onestop
	return $?
}

sssd_restart()
{
	adctl_cmd ${service} sssd onestop
	adctl_cmd ${service} sssd onestart
	return $?
}

cifs_enabled()
{
	srv_enabled cifs && return 0
	return 1
}

cifs_start()
{
	adctl_cmd ${python} ${notifier} call notifier.start cifs
	return $?
}

cifs_stop()
{
	adctl_cmd ${python} ${notifier} call notifier.stop cifs
	return $?
}

cifs_restart()
{
	adctl_cmd ${python} ${notifier} call notifier.stop cifs
	adctl_cmd ${python} ${notifier} call notifier.start cifs
	return $?
}

adctl_start()
{
	local cifs_started=0	
	local ad_started=0

	if ! AD_init
	then
		activedirectory_set 0
		return 1
	fi

	if cifs_enabled
	then
		cifs_started=1
		cifs_stop
	else
		srv_set cifs 1
	fi

	if activedirectory_enabled
	then
		ad_started=1
	else		
		activedirectory_set 1
	fi

	echo ${cifs_started} > "${cifs_file}"

	adctl_cmd ${service} ix-hostname quietstart
	adctl_cmd ${service} ix-kerberos quietstart default "$(AD_get ad_krb_realm)"
	adctl_cmd ${service} ix-nsswitch quietstart
	adctl_cmd ${service} ix-ldap quietstart

	adctl_cmd ${service} ix-kinit quietstart
	if ! adctl_cmd ${service} ix-kinit status
	then
		activedirectory_set 0
		return 1
	fi

	if AD_has_unix_extensions && AD_has_keytab
	then
		adctl_cmd ${service} ix-sssd start
		if sssd_running
		then
			sssd_restart
		else
			sssd_start
		fi
	fi

	cifs_start

	if ! adctl_cmd ${service} ix-activedirectory quietstart
	then
		activedirectory_set 0
		return 1
	fi
	if ! adctl_cmd ${service} ix-activedirectory status
	then
		activedirectory_set 0
		return 1
	fi

	cifs_restart

	adctl_cmd ${service} ix-pam quietstart
	adctl_cmd "${service} ix-cache quietstart &"

	touch "${status_file}"
	return 0
}

adctl_stop()
{
	local cifs_started=1
	local prev_cifs_started=0

	AD_generate_config

	if [ -s "${cifs_file}" ]
	then
		prev_cifs_started="$(cat "${cifs_file}")"
	fi

	if ! activedirectory_enabled
	then
		activedirectory_set 1
	fi

	if cifs_enabled
	then
		cifs_started=1
		cifs_stop
	fi

	if sssd_running
	then
		sssd_stop
		adctl_cmd ${service} ix-sssd start
	fi

	if [ "${prev_cifs_started}" = "0" -a "${cifs_started}" = "0" ]
	then
		adctl_cmd ${service} samba_server forcestop
		srv_set cifs 0
		activedirectory_set 0
		adctl_cmd ${service} ix-pre-samba start

	elif [ "${prev_cifs_started}" = "0" -a "${cifs_started}" = "1" ]
	then
		adctl_cmd ${service} samba_server forcestop
		srv_set cifs 0
		activedirectory_set 0
		adctl_cmd ${service} ix-pre-samba start

	elif [ "${prev_cifs_started}" = "1" -a "${cifs_started}" = "0" ]
	then
		adctl_cmd ${service} samba_server forcestop
		activedirectory_set 0
		srv_set cifs 1
		cifs_start

	elif [ "${prev_cifs_started}" = "1" -a "${cifs_started}" = "1" ]		
	then
		adctl_cmd ${service} samba_server forcestop
		activedirectory_set 0
		srv_set cifs 1
		cifs_start
	fi

	adctl_cmd ${service} ix-kinit forcestop
	activedirectory_set 0
	rm -f "${status_file}"

	adctl_cmd ${service} ix-hostname quietstart
	adctl_cmd ${service} ix-kerberos restart
	adctl_cmd ${service} ix-nsswitch quietstop
	adctl_cmd ${service} ix-pam quietstop
	adctl_cmd "${service} ix-cache quietstop &"
	
	AD_remove_config
	return 0
}

adctl_status()
{
	adctl_cmd ${service} ix-activedirectory status
}

name="adctl"
start_cmd='adctl_start'
status_cmd='adctl_status'
stop_cmd='adctl_stop'
extra_commands='status'

load_rc_config $name
run_rc_command "$1"
